package com.fer.controller;

import com.fer.model.Orders;
import com.fer.service.GoodsService;
import com.fer.service.OrdersService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 这是订单管理的控制层
 * @Fer1g
 */
@Controller
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    //添加订单的方法
    @PostMapping
    @ResponseBody
    public Map addOrder(String order_address,String goods_list,Integer shop_id){
        Map<String,Object> reslut = new HashMap<>();
        Orders orders = new Orders();
        orders.setOrder_address(order_address);
        orders.setShop_id(shop_id);
        String[] list = goods_list.split(",");
        try {
            boolean state = ordersService.insertOrder(orders, list);
            if (state == false){
                reslut.put("code",505);
                reslut.put("message","存在未在售商品");
                return reslut;
            }
        }catch (Exception e){
            reslut.put("code",400);
            reslut.put("message","添加失败");
            return reslut;
        }
        reslut.put("code",200);
        reslut.put("message","添加成功");
        return reslut;
    }

    //删除订单的方法
    @DeleteMapping("/{order_id}")
    @ResponseBody
    public Map deleteOrder(@PathVariable("order_id") Integer order_id){
        Map<String,Object> reslut = new HashMap<>();
        try{
            ordersService.deleteOrder(order_id);
        }catch (Exception e){
            reslut.put("code",400);
            reslut.put("message","删除失败");
            return reslut;
        }
        reslut.put("code",200);
        reslut.put("message","删除成功");
        return reslut;
    }

    //修改订单的方法
    @PutMapping
    @ResponseBody
    public Map update(Orders orders){
        Map<String,Object> reslut = new HashMap<>();
        boolean state = ordersService.updateOrder(orders);
        if (state==false){
            reslut.put("code",400);
            reslut.put("message","修改失败");
            return reslut;
        }
        reslut.put("code",200);
        reslut.put("message","修改成功");
        return reslut;
    }

    //查询订单的方法
    //这个是查询分类别订单的方法
    @GetMapping("/{shop_id}/{is_finished}")
    @ResponseBody
    public Map getOrder(@PathVariable("is_finished") boolean is_finished,@PathVariable("shop_id") Integer shop_id){
        System.out.println(is_finished);
        Map<String,Object> reslut = new HashMap<>();
        List<Orders> ordersList = ordersService.selectOrdersByState(is_finished,shop_id);
        if (ordersList.size()==0){
            reslut.put("code",400);
            reslut.put("message","查找失败");
            return reslut;
        }
        reslut.put("code",200);
        reslut.put("message","查找成功");
        reslut.put("data",ordersList);
        return  reslut;
    }
    //查询所有订单的方法
    @GetMapping("/{shop_id}")
    @ResponseBody
    public Map getAllOrder(@PathVariable("shop_id") Integer shop_id){
        Map<String,Object> reslut = new HashMap<>();
        List<Orders> ordersList = ordersService.selectAllOrder(shop_id);
        System.out.println(ordersList);
        if (ordersList.size()==0){
            reslut.put("code",400);
            reslut.put("message","查找失败");
            return reslut;
        }
        reslut.put("code",200);
        reslut.put("message","查找成功");
        reslut.put("data",ordersList);
        return reslut;
    }
}
